package cuiyt.leetcode.dichotomy.democ;

/**
 * @author cyt
 * @create 2020-09-21 17:37
 */
public class Tb {
    public static void main(String[] args) {
        int[] ints = {5, 7, 7, 8, 8, 10};
        Tb tb = new Tb();
        int[] range = tb.searchRange(ints, 8);
        for (int i : range) {
            System.out.print(i + " ");
        }
    }

    public int[] searchRange(int[] nums, int target) {
        int length = nums.length;
        int left = 0;
        int right = length - 1;
        int[] ans = new int[]{-1, -1};
        int k = 0;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                int r = mid;
                int l = mid;
                while (l > 0 && nums[l] == nums[l - 1]) {
                    l--;
                }
                while (r < length - 1 && nums[r] == nums[r + 1]) {
                    r++;
                }
                ans[0] = l;
                ans[1] = r;
            }
            if (nums[mid] > target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return ans;
    }
}
